/*
 * This file is part of Space Invaders: Rehash
 * Copyright (C)2007 Vicent Marti (tanoku@gmail.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>

#include "SDL.h"
#include "SDL_ttf.h"
#include "SDL_opengl.h"

#include "game.h"
#include "main.h"
#include "gfx.h"
#include "font_handler.h"
#include "sprites.h"
#include "game_const.h"

Missile::Missile( Game *parent, int x, int y, int speed, float angle, int number )
{
	m_x = x;
	m_y = y;
	m_speed = -speed;
	m_angle = angle;
	m_number = number;  
	m_parent = parent;
	m_rotation = 0.0f;
	
	if ( m_number == -1 )
		strcpy(m_numberStr, "P");
	else sprintf(m_numberStr, "%d", m_number);
	
	m_disabled = false;
	
	m_smokeTimer.start();
	m_smokeCount = 0;
	
	m_trailSound = Mix_PlayChannel( -1, g_Sounds[SOUND_MISSILE], 0 );
}

void Missile::render()
{	
	float x,y,z;
	glScreenToWorld( m_x, m_y, x, y, z );
	
	if (m_parent->getGameRules() & GR_COLORSHOOTER)
	{
		float r, g, b;
		m_parent->translateColor(m_number, r, g, b);
//		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glDrawModel( g_Models[1], x, y, z, g_Textures[17], r, g, b, 0.010f, m_rotation, 0.0, m_disabled?0.0f:1.0f, m_disabled?1.0f:0.0f );
//		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	}
	else
	{
//		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
		glDrawModel( g_Models[1], x, y, z, g_Textures[17], 1.0f, 1.0f, 1.0f, 0.010f, m_rotation, 0.0, m_disabled?0.0f:1.0f, m_disabled?1.0f:0.0f );
//		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
		
		if ( m_number != 0 && !m_disabled )
		{
			glEnable2D();
			if ( m_number > 9 )
				glPrint( FONT_ALIEN, m_x - 14, m_y + 16, m_numberStr);
			else glPrint( FONT_ALIEN, m_x - 8, m_y + 16, m_numberStr); 
			glDisable2D();
		}       
	}
	
	m_rotation += m_disabled?5.5f:3.5f;
}

void Missile::tick()
{
	m_y += (int)(m_speed*sin((float)m_angle*PI/180.0));
	
	if ( m_angle != 90.0 )
		m_x += (int)(m_speed*cos((float)m_angle*PI/180.0));
	
	this->render();

	if ( !m_disabled && m_smokeTimer.get_ticks() > 50 )
	{
		m_parent->newSprite( SPRITE_SMOKETRAIL, m_x, m_y + 32, 64 - 4*m_smokeCount );
		m_smokeCount++;
		m_smokeTimer.start();
	}
	
	if ( m_disabled && m_trailSound != -1 )       
	{   
	     Mix_HaltChannel(m_trailSound);
	     m_trailSound = -1;
	}
}

bool Missile::colCheck()
{
	SDL_Rect ab;
	
	if ( (m_parent->getGameRules() & GR_MISSILES_BOUNCE_ON_TOP) && m_y < 8 )
	{
		m_angle = 270.0f + ((rand()%2==0)?7.5f:-7.5f) + ((rand()%90)*0.5f - 22.5f)*1.125f;
		m_speed = -4;
		m_disabled = true;
		return false;
	}
	
	if ( m_y < 0 || m_y > SCREEN_H || m_x < 0 || m_x > SCREEN_W )
	{
		return true;
	}
	
	for ( int i=0; i<m_parent->getBarrierCount(); ++i )
	{
		if ( m_parent->getBarrierBounds( i, &ab ) )
		{
			if ( m_x > ab.x && m_x < ab.x+ab.w && m_y > ab.y && m_y < ab.y+ab.h )
			{
				m_parent->barrierHit(i, 11 + rand()%7);
				return true;
			}
		}
	}
	
	if ( m_disabled )
	{
		Player *pl = m_parent->getPlayer();
		if ( pl )
		{
			pl->getBounds( &ab );
			if ( m_x > ab.x && m_x < ab.x+ab.w && m_y > ab.y && m_y < ab.y+ab.h )
			{
				pl->hit(15 + rand()%7);
				return true;
			}
		}

		if ( m_y > SCREEN_H - 32 ) 
			return true;
		
		return false;
	}

	Alien *alien; int count = 0;
	while ( (alien = m_parent->getEnemy(count)) != NULL )
	{   
		alien->getBounds(&ab);
		if ( m_x > ab.x && m_x < ab.x+ab.w && m_y > ab.y && m_y < ab.y+ab.h )
		{
			if ( alien->hit(this) ) return true;
			else if ( m_parent->getGameRules() & GR_MISSILES_BOUNCE_ON_ENEMIES )
			{
				m_angle = 270.0f + rand()%30 - 15.0f; //((rand()%2==0)?7.5f:-7.5f) + ((rand()%90)*0.5f - 22.5f)*1.125f;
				m_speed = -4;
				m_disabled = true;
			}
			else
			{
				return true;
			}
		}
		
		count++;
	}
	
	return false;
}
